import { Entity, Column, Index, OneToMany } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { Chapter } from './chapter.entity';

@Entity('documents')
@Index('idx_status', ['status'])
export class Document extends BaseEntity {
  @Column({
    type: 'varchar',
    length: 200,
    comment: '文档标题',
  })
  title: string;

  @Column({
    type: 'text',
    nullable: true,
    comment: '文档简介',
  })
  description?: string;

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '封面图',
  })
  cover?: string;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    default: 0,
    comment: '单独购买价格',
  })
  price: number;

  @Column({
    type: 'enum',
    enum: ['free', 'vip', 'svip'],
    default: 'vip',
    comment: '最低会员要求',
  })
  minMembership: 'free' | 'vip' | 'svip';

  @Column({
    type: 'int',
    default: 2,
    comment: '免费试看章节数',
  })
  freeChapterCount: number;

  @Column({
    type: 'enum',
    enum: ['draft', 'published', 'offline'],
    default: 'draft',
    comment: '状态',
  })
  status: 'draft' | 'published' | 'offline';

  @Column({
    type: 'int',
    default: 0,
    comment: '浏览量',
  })
  views: number;

  // 关联
  @OneToMany(() => Chapter, (chapter) => chapter.document)
  chapters: Chapter[];
}
